package cn.cs.yangtai.controller;

import cn.cs.yangtai.entity.Blog;
import cn.cs.yangtai.entity.Order;
import cn.cs.yangtai.entity.vo.CommentVo;
import cn.cs.yangtai.entity.vo.OrderVo;
import cn.cs.yangtai.entity.vo.PageResult;
import cn.cs.yangtai.entity.vo.ResponsePojo;
import cn.cs.yangtai.service.OrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * (Order)表控制层
 *
 * @author makejava
 * @since 2024-12-12 09:22:18
 */
@RestController
@CrossOrigin
@RequestMapping("order")
@Tag(name = "订单")
public class OrderController {
    /**
     * 服务对象
     */
    @Resource
    private OrderService orderService;

    /**
     * 分页查询
     *
     * @param cuttentPage 本页
     * @param pageSize    分页大小
     * @return 查询结果
     */
    @Operation(summary = "订单分页")
    @GetMapping
    public ResponsePojo<PageResult<OrderVo>> queryByPage(Integer cuttentPage, Integer pageSize) {
        PageResult<OrderVo> result=orderService.queryByPage(cuttentPage,pageSize);
        return ResponsePojo.success(result);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<OrderVo> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.orderService.queryById(id));
    }

    /**
     * 根据用户查询订单数据
     * @return 对象列表
     */
    @Operation(summary = "查询用户的订单")
    @GetMapping("/UserOrder")
    public List<OrderVo> Orders(Integer userid) {
        return orderService.queryByUserid(userid);
    }


    /**
     * 新增数据
     *
     * @return 新增结果
     */
    @Operation(summary = "添加订单")
    @PostMapping
    public ResponseEntity<Order> add(Integer orderUserId, Double orderPrice, Integer orderStatus, String orderBei,String orderXuan) {
        Order order=new Order();
        order.setOrderUserId(orderUserId);
        order.setOrderPrice(orderPrice);
        order.setOrderStatus(orderStatus);
        order.setOrderDate(new Date());
        order.setOrderBei(orderBei);
        order.setOrderXuan(orderXuan);
        return ResponseEntity.ok(this.orderService.insert(order));
    }

    /**
     * 编辑数据
     *
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<OrderVo> edit(Integer orderId, Double orderPrice,Integer orderStatus,String orderBei,String orderXuan) {
        Order order=new Order();
        order.setOrderId(orderId);
        order.setOrderPrice(orderPrice);
        order.setOrderStatus(orderStatus);
        order.setOrderDate(new Date());
        order.setOrderBei(orderBei);
        order.setOrderXuan(orderXuan);
        return ResponseEntity.ok(this.orderService.update(order));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.orderService.deleteById(id));
    }

}

